﻿@using Models
@model OneNewsWithComments
@{
    var cookie = Request.Cookies[FormsAuthentication.FormsCookieName];
    FormsAuthenticationTicket auth = null;
    if (cookie != null)
    {
        auth = FormsAuthentication.Decrypt(cookie.Value);

        ViewBag.auth = auth;
    }
    
    ViewBag.Title = Model.News.Title;
    var news = Model.News;
}
@Html.Partial("_openDialog")

@if (Model == null)
{
    <span>Invalid news id</span>
}
else
{

    <div id="newsBorder">
        <div id="oneNews">
            <div id="newsName">
                <b>News title:</b> @news.Title
            </div>
            <div id="newsAuthor">
                <a href="/Users/Index/@news.Author">@news.Author</a>
            </div>
            <div id="newsText">
                <br />
                @{
                    @news.Text

                }
            </div>
            <table id="newtbl" width="100%" border="0" cellpadding="0" cellspacing="0" style="border-color: transparent">
                <tr>
                    <td>
        
                        <div id="newsRating">
                            Rating: @news.Rating
                            @if (auth != null)
                            {
                                if (!ViewBag.VoteOrNot)
                                {
                                    using (Html.BeginForm())
                                    {
                                        @Html.DropDownList("rating", new SelectList(new List<int> {1, 2, 3, 4, 5}, 1))
                                        @Html.Hidden("id", Model.News.Id)
                                        @Html.Hidden("login", auth.Name)
                                        <input type="submit" value="Rate" name="subRating" />
                                    }
                                }
                                else
                                {
                                    <span>You allready voted</span>
                                }
                            }
                            else
                            {
                                <span>You cant vote</span>
                            }
                        </div>
       
                        <div id="newsDate">
                            @if (news.UpdateDate != null)
                            {

                                <span>Date of last update: @news.UpdateDate.Value.GetDateTimeFormats()[15]</span>
                            }
                            else
                            {
                                if (news.PublishDate != null)
                                {
                                    <span>Date of publishing: @news.PublishDate.Value.GetDateTimeFormats()[15] </span>
                                }
                            }
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        @if(Model.News.Images != null)
                        {
                            <span>Attached images</span><br/>
                            int i = 1;
                            foreach (ImageInNews image in Model.News.Images)
                            {
                                <a href="@image.Url" target="blank"><img id="image_in_news" src="@image.Url" alt="image"></a> 
                                if(i % 3 == 0)
                                {
                                    <br/>
                                }
                                i++;
                            }

                        } 
                    </td>
                </tr>
                <tr>
                    <td>
                        <div id="tags">
                            @{

                                if (Model.News.TagsList != null)
                                {
                                    foreach (Tag tag in Model.News.TagsList)
                                    {
                                        <div id="onetag">
                                            @Html.ActionLink(tag.Name, "Index", "Tags", new { id = tag.Name }, null)
                                        </div>

                                    }
                                }
                            }
                        </div>
                    </td>
                </tr>
            </table>
            
                       @if (auth != null)
                       {
                           if (auth.UserData == "2" || auth.UserData == "1")
                           {
                                @Html.ActionLink("Update", "updateNews", "admin", new { id = news.Id }, null)

                                <span>
                                </span>

                                if (auth != null && auth.UserData == "1")
                                {
                                    @Html.ActionLink("Delete", "DeleteNews", "admin", null, new { onclick = "return openDialog(" + news.Id + ")" })
                                }
                           }
                       }
        </div>
    
    </div>


    
    <div id="comments_Block">
    @if (Model.Comments != null)
    {
       
            <span><h3>Comments</h3></span>

        foreach (Comment comment in Model.Comments)
        {

            <div id="comments_border">
                <div id="comments">
                    <div id="comments_Author"><a href="/Users/Index/@comment.Author.Login"><img src="@comment.Author.Image" id="image_in_comment"/></a></div>
                    <div id="comments_Title">@comment.Title</div>
                    <div id="comments_PublishDate">@comment.PublishDate.GetDateTimeFormats()[15]</div>
                    <div id="comments_Text">@comment.Text</div>
                </div>
                   
            </div>
            <div style="margin-top: 10px">
                 @if(auth != null)
                    {
                        if (auth.UserData == "1" || auth.UserData == "2")
                        {
                            <a href="/Admin/EditComment/@comment.Id">Edit</a>
                            <span></span>
                            <a href="/Admin/DeleteComment/@comment.Id">Delete</a>
                        }
                    }
            </div>
                <div id="comments_separator"></div>

        }
    }
            @if (auth != null)
            {
                <div id="comments_Add">
                    @using (Html.BeginForm())
                    {
                        <br/>
                        <span>Add comment</span>
                        <br/>
                        <table border="0" cellpadding="2">
                            <tr>
                                <td>Title</td>
                                <td>@Html.TextBoxFor(x => x.AddComment.Title)</td>
                            </tr>
                            <tr>
                                <td>
                                    Text
                                </td>
                                <td>
                                    @Html.TextAreaFor(x => x.AddComment.Text)
                                    @Html.Hidden("AddComment.Author.Login", auth.Name)
                                    @Html.Hidden("AddComment.NewsId", Model.News.Id)
                                    @Html.Hidden("AddComment.PublishDate", DateTime.Now)
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2">
                                    <input type="submit" name="subAddComment" value="Add"/>
                                </td>
                            </tr>
                        </table>
                    }
                </div>
            }
            </div>
      }


